//
//  KHttpRequestTool.m
//  NewProjectImage
//
//  Created by apple on 2017/9/6.
//  Copyright © 2017年 铁科院. All rights reserved.
//

#import "KHttpRequestTool.h"
#import "TKYLoginController.h"
#import <UIKit/UIKit.h>
#import <Foundation/Foundation.h>
@implementation KHttpRequestTool
+ (void)POST:(NSString *)URLString parameters:(id )parameters success:(void(^)(id response))success failure:(void(^)(NSError *error))failure
{
       AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
    //如果报接受类型不一致请替换一致text/html或别的
    //申明返回的结果是json类型
    // manager.requestSerializer=[AFJSONRequestSerializer serializer];
    //声明请求的数据是json类型
    // manager.responseSerializer=[AFJSONResponseSerializer serializer];
//    manager.responseSerializer.acceptableContentTypes = [NSSet setWithObjects:@"application/json", @"text/html",@"text/json",@"text/javascript",@"text/plain",nil];
    [manager POST:URLString parameters:parameters progress:^(NSProgress * _Nonnull uploadProgress) {

    } success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        
        success(responseObject);

    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        failure(error);

    }];

}

+ (void)GET:(NSString *)URLString parameters:(id )parameters success:(void(^)(id response))success failure:(void(^)(NSError *error))failure
{
    AFHTTPSessionManager *mgr = [AFHTTPSessionManager manager];
    //申明返回的结果是json类型
    mgr.requestSerializer=[AFJSONRequestSerializer serializer];
    //声明请求的数据是json类型
    mgr.responseSerializer=[AFJSONResponseSerializer serializer];
    //如果报接受类型不一致请替换一致text/html或别的
    mgr.responseSerializer.acceptableContentTypes = [NSSet setWithObjects:@"application/json", @"text/html",@"text/json",@"text/javascript",@"text/plain",nil];
    
    // https ================
    //    AFSecurityPolicy *securityPolicy = [AFSecurityPolicy policyWithPinningMode:AFSSLPinningModeCertificate];// 证书模式
    //    NSString *cerPath = [[NSBundle mainBundle] pathForResource:@"Test" ofType:@"cer"];
    //    NSData *cerData = [NSData dataWithContentsOfFile:cerPath];
    //      NSSet *set = [[NSSet alloc] initWithObjects:cerData, nil];
    //    securityPolicy.pinnedCertificates = set;// 配置证书
    //    securityPolicy.allowInvalidCertificates = YES;// 客户端是否信任非法证书
    //    [securityPolicy setValidatesDomainName:NO];// 是否在证书域字段中验证域名
    //    mgr.securityPolicy = securityPolicy;
    //
    //    [mgr.requestSerializer setValue:@"application/x-www-form-urlencoded;charset=UTF-8" forHTTPHeaderField:@"content -type"];
    //添加切换动画
    UIWindow *window = [[[UIApplication sharedApplication] delegate] window];  // 获得根窗口
    [mgr GET:URLString parameters:parameters progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        if ([responseObject[@"code"] integerValue] == 401) {
            [SVProgressHUD showSVProgressImage:nil Duration:3.0 FontSize:16 status:responseObject[@"msg"]];
            //弹出登录界面
            TKYLoginController *login = [[TKYLoginController alloc]init];
            [window removeFromSuperview];
            window.rootViewController = login;
            return ;
        }
        success(responseObject);
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        failure(error);
    }];
}


@end
